Jingqi Huang, [NAME 2]
# install.packages("ggplot2")
# install.packages("plotly")
library(ggplot2)
library(plotly)
library(dplyr)
df <- data.frame(type =rep(c("A", "B"), each=500), subtype=rep(c("1", "2"), each=250), value = rnorm(1000))
p <- ggplot(df, aes(x=value, fill=subtype))+
geom_histogram(position="identity", alpha=0.5, binwidth=0.2)+
facet_grid(~type)
ggplotly(p)
p <- plot_ly(x = filter(df, type=="A")$value, y = filter(df, type=="B")$value)
p2<-subplot(p %>% add_markers(alpha=0.4), p%>%add_trace(type='histogram2dcontour'), p%>%add_histogram2d())
p2
plot_ly(diamonds, x = ~price/carat, y = ~clarity, color = ~clarity, type = "box") %>%
layout(title="Interactive BoxPlot with Plotly")
# Or
p <- ggplot(diamonds, aes(x=clarity, y=price/carat, color=clarity)) +
geom_boxplot() +
coord_flip() +
ggtitle("BoxPlot with ggplot2")
# ggplotly(p), also generate interactive graph
plot_ly(filter(diamonds, color=='D'), x = ~carat, y = ~price, color = ~clarity,
marker = list(size=4, opacity=0.5),
# hover text
text = ~paste("Price: ", price, "$<br>Cut: ", cut, "<br>Clarity: ", clarity)) %>%
# set title and axis
layout(title="Interactive Scatter Plot with Plotly")
plot_ly(diamonds[sample(nrow(diamonds), 1000), ], x=~price/carat, y=~table, z=~depth, color=~cut,
marker = list(size=4, opacity=0.5))%>%
layout(title="Interactive 3D Scatter Plot with Plotly")
mtcars <- mutate(mtcars, type = case_when(mtcars$am == 0 ~ "Auto", mtcars$am == 1 ~ "Manual"))
plot_ly(mtcars, x=~mpg, y=~wt, z=~hp, color=~type) %>%
layout(title="Interactive 3D Scatter Plot with Plotly", scene = list(xaxis = list(title = "mpg"), yaxis = list(title = "weight"), zaxis = list(title = "horsepower")))
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
a <- rnorm(100, 5, 1)
b <- rnorm(100, 0, 1)
c <- rnorm(100, -5, 1)
df <- data.frame(x=c(1:100), a, b, c)
plot_ly(df, x = ~x) %>%
add_trace(y = ~a, name="line", type="scatter", mode = "lines", line=list(color='rgb(23, 54, 211)', width=2)) %>%
add_trace(y=~b, name="dot line with markers", mode = "lines+markers", line=list(dash='dot')) %>%
add_trace(y=~c, name="scatter markers only", mode = "markers") %>% #same as scatter plot
layout(title="Interactive Line Plot with Plotly", yaxis=list(title="value"))
# plot_ly(diamonds, x=~cut, color=~clarity, type="histogram")
plot_ly(count(diamonds, cut, clarity), x=~cut, y=~n, color=~clarity, type="bar", text=~n, marker=list(opacity=0.4, line=list(color='rgba(8,48,148, 1)', width=1.5))) %>%
layout(barmode = 'group')
question <- c('The course was effectively<br>organized',
'The course developed my<br>abilities and skills for<br>the subject',
'I would recommend this<br>course to a friend',
'Any other questions')
df <- data.frame(question, sa=c(21, 24, 27, 29), a=c(30, 31, 26, 24), ne=c(21, 19, 23, 15), ds=c(16, 15, 11, 18), sds=c(12, 11, 13, 14))
answer_label <- c('Strongly<br>agree', 'Agree', 'Neutral', 'Disagree', 'Strongly<br>disagree')
p <- plot_ly(df, x=~sa, y=~question, type="bar", orientation="h", marker = list(color = 'rgba(38, 24, 74, 0.8)',
line = list(color = 'rgb(248, 248, 249)', width = 1)))
p <- p %>% add_trace(x=~a, marker = list(color = 'rgba(71, 58, 131, 0.8)'))
p <- p %>% add_trace(x=~ne, marker = list(color = 'rgba(122, 120, 168, 0.8)'))
p <- p %>% add_trace(x=~ds, marker = list(color = 'rgba(164, 163, 204, 0.85)'))
p <- p %>% add_trace(x=~sds, marker = list(color = 'rgba(190, 192, 213, 1)'))
p <- p %>% layout(barmode='stack',
xaxis = list(title = "", showgrid = FALSE,showticklabels = FALSE, zeroline = FALSE),
yaxis=list(title=""),
paper_bgcolor = 'rgb(248, 248, 255)', plot_bgcolor = 'rgb(248, 248, 255)', margin = list(l = 120, r = 10, t = 40, b = 10),showlegend = FALSE)
p <- p %>% add_annotations(xref='x', yref='y', x=~sa/2, y=~question, text=paste(df[,"sa"], '%'), font=list(size=12, color="white"), showarrow=FALSE) %>%
add_annotations(x=~sa+a/2, text=paste(df[,"a"], '%'), font=list(size=12, color="white"), showarrow=FALSE)%>%
add_annotations(x=~sa+a+ne/2, text=paste(df[,"ne"], '%'), font=list(size=12, color="white"), showarrow=FALSE)%>%
add_annotations(x=~sa+a+ne+ds/2, text=paste(df[,"ds"], '%'), font=list(size=12, color="white"), showarrow=FALSE)%>%
add_annotations(x=~sa+a+ne+ds+sds/2, text=paste(df[,"sds"], '%'), font=list(size=12, color="white"), showarrow=FALSE)
p <- p %>% add_annotations(xref = 'x', yref = 'paper',
x = c(21 / 2, 21 + 30 / 2, 21 + 30 + 21 / 2, 21 + 30 + 21 + 16 / 2,
21 + 30 + 21 + 16 + 12 / 2), y = 1.05,
text = answer_label,
font = list(size = 12, color = 'rgb(67, 67, 67)'), showarrow = FALSE)
p